#!/usr/bin/env python
import os, sys
from file_manager_class import FileManager
from liwe.decorators import dataset

def get_templates ( liwe ):
	n = FileManager ( liwe )
	return { "templates" : n.get_templates () }

def file_add ( liwe, file_name = '', base_dir = '/', id_folder = None, module = '', id_obj = '', overwrite = False ):
	"""
	:INPUT:
		- base_dir:	SAFE_STRING
		- id_folder:	SAFE_NUMBER
		- file_name:	SAFE_DB_STRING
		- module:	SAFE_DB_STRING
		- id_obj:	SAFE_DB_STRING
		- overwrite:	SAFE_BOOL
	"""

	f = FileManager ( liwe )

	if id_folder is None:
		id_folder = f.node_from_path ( base_dir )

	for fil in liwe.pyhp.uploads:	
		fil = liwe.pyhp.uploads [ fil ]

		sys.stderr.write ( "%s\n" % fil )
		if not file_name:
			fname = fil [ 'filename' ]
		else:
			fname = file_name

		f.file_add ( 	id_folder = id_folder, 
			     	file_name = fname, 
				file_obj = fil [ 'file' ],	
			     	module = module, 
			     	id_obj = id_obj )

@dataset	
def ds ( liwe, id_folder = 0, show_dirs = False, show_files = False, module = '', id_obj = '', date_start = '', date_end = '', start_point = 0, limit = 10 ):
	"""
	:INPUT:
		- id_folder:	SAFE_NUMBER
		- show_dirs:	SAFE_BOOL
		- show_files:	SAFE_BOOL
		- module:	SAFE_DB_STRING
		- id_obj:	SAFE_DB_STRING
		- date_start:	SAFE_DB_STRING
		- date_end:	SAFE_DB_STRING
		- start_point:	SAFE_NUMBER
		- limit:	SAFE_NUMBER
	"""

	f = FileManager ( liwe )
	res = f.ds ( id_folder = id_folder, show_dirs = show_dirs, show_files = show_files, module = module, id_obj = id_obj, 
			date_start = date_start, date_end = date_end, start_point = start_point, limit = limit )
	
	for row in res [ 'rows' ]:	
		if row [ 'folder' ]:
			row [ 'thumb' ] = '/modules/file_manager/gfx/folder.png'
			continue
		fname = f._get_file_name ( row [ 'id_node' ] ) + '.thumb.jpg'
		if not os.path.exists ( fname ):
			fname = '/modules/file_manager/gfx/empty.png'
		row [ 'thumb' ] = fname

	return res

def ls ( liwe, id_folder = 0, show_dirs = False, show_files = False, module = '', id_obj = '', tree = False ):
	"""
	:INPUT:
		- id_folder:	SAFE_NUMBER
		- show_dirs:	SAFE_BOOL
		- show_files:	SAFE_BOOL
		- module:	SAFE_DB_STRING
		- id_obj:	SAFE_DB_STRING
		- tree:		SAFE_BOOL
	"""

	f = FileManager ( liwe )

	res = f.ls ( id_folder = id_folder, show_dirs = show_dirs, show_files = show_files, module = module, id_obj = id_obj )
	for row in res:
		if row [ 'folder' ]:
			row [ 'thumb' ] = '/modules/file_manager/gfx/folder.png'
			continue
		fname = f._get_file_name ( row [ 'id_node' ] ) + '.thumb.jpg'
		if not os.path.exists ( fname ):
			fname = '/modules/file_manager/gfx/empty.png'
		row [ 'thumb' ] = fname

	if tree:
		lst = res
		res = []
		for row in lst:
			res.append ( dict ( text = row [ 'name' ], val = row [ 'id_node' ], folder = row [ 'folder' ] ) )

	return { "nodes" : res }

def folder_create ( liwe, folder_name, id_parent = 0, module = '', id_obj = '' ):
	"""
	:INPUT:
		- folder_name:	SAFE_DB_STRING
		- id_parent:	SAFE_NUMBER
		- module:	SAFE_DB_STRING
		- id_obj:	SAFE_DB_STRING
	"""
	f = FileManager ( liwe )
	f.folder_create ( folder_name = folder_name, id_parent = id_parent, module = module, id_obj = id_obj )

	return { "res" : "OK" }

def remove ( liwe, id_nodes ):
	"""
	:INPUT:
		- id_nodes:	SAFE_STRING
	"""
	f = FileManager ( liwe )
	nodes = id_nodes.split ( '|' )
	for n in nodes:
		try:
			n = int ( n, 10 )
			f.remove ( id_node = n )
		except:
			pass

	return { "res" : "OK" }

def rename ( liwe, id_node, name ):
	"""
	:INPUT:
		- id_node:	SAFE_NUMBER
		- name:		SAFE_DB_STRING
	"""
	f = FileManager ( liwe )
	f.rename ( id_node = id_node, name = name )

	return { "res" : "OK" }

def stat ( liwe, id_nodes, full_path = 0 ):
	"""
	:INPUT:
		- id_nodes:	SAFE_STRING
		- full_path:	SAFE_BOOL
	"""
	f = FileManager ( liwe )

	id_nodes = [ str ( int ( x ) ) for x in id_nodes.split ( "|" ) ]

	res = f.stat_multi ( id_nodes = id_nodes, full_path = full_path )

	return { "stat" : res }
